package stream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {

    /*
        获取流的三种方式

     */
    public static void main(String[] args) {
        //1.通过集合获取
        ArrayList<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        Stream<String> stream = list.stream();//单线程流
        stream.forEach(System.out::println);
        System.out.println("------------------------------------------");
        Stream<String> parallelStream = list.parallelStream();//多线程流
        parallelStream.forEach(System.out::println);
        System.out.println("------------------------------------------");

        //2.通过数组获取,通过Stream的静态方法
        String[] arr = new String[]{"张三","李四","王五"};
        Stream<String> stream1 = Stream.of(arr);
        stream1.forEach(System.out::println);
        System.out.println("------------------------------------------");
        Stream<Integer> stream2 = Stream.generate(() -> 10).limit(5);//通过generate方法生成一个流，通过limit方法限制数量，输出5个10
        stream2.forEach(System.out::println);
        System.out.println("------------------------------------------");
        Stream<Integer> stream3 = Stream.iterate(1,x->x+2).limit(5);//一直输出x+2，输出5个limit就是限制的数量
        stream3.forEach(System.out::println);
        System.out.println("------------------------------------------");

        //3.将流转成集合
        Stream<String> stream4 = list.stream();
        List<String> collect = stream4.collect(Collectors.toList());
    }
}
